package com.bxd.baodan.mvp.login;

import android.content.Intent;

import com.bxd.baodan.App;
import com.bxd.baodan.activity.LoginActivity;
import com.bxd.baodan.app.Constant;
import com.bxd.baodan.app.JsonApi;
import com.bxd.baodan.app.LoginInfo;
import com.bxd.baodan.app.SharedPreferanceKey;
import com.bxd.baodan.item.UserItem;
import com.bxd.baodan.util.AppManager;
import com.bxd.baodan.util.JSONUtil;
import com.bxd.baodan.util.LoginIMUtil;
import com.bxd.baodan.util.NetUtil;
import com.bxd.baodan.util.SaveMsgUtil;
import com.bxd.baodan.util.SpUtil;
import com.bxd.baodan.util.Utils;

/**
 * Created by Administrator on 2017/5/23.
 */

public class LoginPresenter extends LoginContract.Presenter {

    public LoginPresenter(LoginContract.View view) {
        this.v = view;
    }

    @Override
    public void startLogin() {

        NetUtil netUtil = new NetUtil(v.getAty(), JsonApi.LOGIN);

        netUtil.setParams("telphone", v.getPhone());
        netUtil.setParams("password", v.getPassWord());
        netUtil.setParams("vcode", v.getVcode());
        netUtil.setParams("type", v.getIsVcode());
        netUtil.postRequest("正在登录", new NetUtil.RequestStringListener() {
            @Override
            public void onComplete(String result) {
                App.getContext().initDefaultValues();
                saveData(result);
                loginIM();  //IM登录
            }
        });
    }

    public void saveData(String result) {
        LoginInfo loginInfo = LoginInfo.getInstance(v.getAty());
        loginInfo.saveLoginInfo(v.getPhone(), v.getPassWord());
        loginInfo.saveUserInfo(JSONUtil.getData(result),
                App.getContext());
        SpUtil.getInstance().savaString(
                SharedPreferanceKey.ACCOUNT,
                loginInfo.getAccount());
        UserItem user = LoginInfo.getInstance(
                App.getContext()).getUser();
        LoginInfo
                .getInstance(App.getContext())
                .getUser().setPhone(v.getPhone());
        SaveMsgUtil.updateLocalInfo(user,
                App.getContext());// 更新用户的登录资料到本地
        App.getContext().sendBroadcast(
                new Intent(Constant.ACTION_SUCCESS_LOGIN));
        App.getContext().sendBroadcast(
                new Intent(Constant.ACTION_SUCCESS_UPLOAD_INFOR));
        if (LoginActivity.question == null
                || LoginActivity.question.equals("")) {
            if (v != null) {
                v.getAty().finish();
            }
        } else {
            submitQuestion();
        }
    }

    /**
     * 验证成功后提交问题
     */
    @Override
    public void submitQuestion() {

        NetUtil netUtil = new NetUtil(v.getAty(), JsonApi.ASK_QUESTION);
        netUtil.setParams("options", LoginActivity.question);
        netUtil.setParams("content", LoginActivity.content);

        netUtil.postRequest("问题提交中", new NetUtil.RequestStringListener() {
            @Override
            public void onComplete(String result) {
                v.askSuccess(result);
            }
        });
    }

    public void loginIM() {
        LoginIMUtil loginUtil = new LoginIMUtil(AppManager.getInstance().currentActivity());
        loginUtil.initIM();
        loginUtil.setCallback(new LoginIMUtil.Callback() {
            @Override
            public void next() {
            }
        });
        loginUtil.loginIM();
    }


    @Override
    public void checkSubmit() {

        NetUtil netUtil = new NetUtil(v.getAty(), JsonApi.LOGIN);
        netUtil.setParams("telphone", v.getPhone());
        netUtil.setParams("password", v.getPassWord());
        netUtil.setParams("vcode", v.getVcode());
        netUtil.setParams("type", v.getIsVcode());
        netUtil.setParams("channelid", Utils.getAppMetaData(App.getContext(), "UMENG_CHANNEL"));
        netUtil.postRequest("正在登录", new NetUtil.RequestStringListener() {
            @Override
            public void onComplete(String result) {
                saveData(result);
            }
        });
    }

    /**
     * 获取验证码
     */
    @Override
    public void attemptIdentifyCode() {
        NetUtil netUtil = new NetUtil(v.getAty(), JsonApi.GET_CHECK_VCODE);
        netUtil.setParams("phone", v.getPhone());
        netUtil.setParams("src", "3");

        netUtil.postRequest("正在获取验证码", new NetUtil.RequestStringListener() {
            @Override
            public void onComplete(String result) {
                v.identifyCode(result);
            }
        });
    }

}
